package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Dept;
import com.sky.domain.Employee;
import com.sky.domain.Job;
import com.sky.service.DeptService;
import com.sky.service.EmployeeService;
import com.sky.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Api(tags = "员工模块")
@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private JobService jobService;

    @GetMapping("/emp")
    public Employee employee(){
        return new Employee();
    }

    // 获取所有员工信息，包含分页查询 模糊查询
    @ApiOperation("获取所有员工")
    @GetMapping("/list")
    public String findAll(@RequestParam(value = "content",defaultValue = "") String name,
                          @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                          Model model){
        PageHelper.startPage(pn,4);
        List<Employee> employeeList = employeeService.findAll(name);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        model.addAttribute("pageInfo",pageInfo);
        return "emp/list";
    }

    // 跳转添加/更新页面
    @ApiOperation("跳转添加/更新页面")
    @GetMapping("/add")
    public String toAddPage(Integer id,Model model){
        if (id!= null){
            Employee emp = employeeService.findEmpById(id);
            model.addAttribute("emp",emp);
            model.addAttribute("msg","1");
        }else{
            model.addAttribute("msg","0");
        }
        List<Job> jobList = jobService.findAll("");
        List<Dept> deptList = deptService.deptList("");
        model.addAttribute("jobList",jobList);
        model.addAttribute("deptList",deptList);
        return "emp/add";
    }

    // 更新/添加操作
    @ApiOperation("更新/添加操作")
    @PostMapping("/add")
    public String insert(Integer id,String gender,Employee employee,Model model){
        if (id != null){
            if ("男".equals(gender)){
                employee.setSex(1);
            }else{
                employee.setSex(0);
            }
            employeeService.update(employee);
        }else{
            employeeService.insert(employee);
        }
        return "redirect:/emp/list";
    }

    // 删除操作
    @ApiOperation("删除操作")
    @GetMapping("/delete")
    public String delete(Integer id){
        employeeService.delete(id);
        return "redirect:/emp/list";
    }

}
